#include "Timebar.h"
#include <QPainter>
#include <QWheelEvent>

TimeBar::TimeBar(QWidget *parent, Qt::WindowFlags f)
  : QWidget(parent, f)
  , _curWheelValue(0)
  , _curDialValue(50.0)
  , _minDialValue(0)
  , _maxDialValue(5000)
  , _dialGap(6)
  , _isDragging(false)
  , _reckText(15, 10, 200, 30)
  , _textSideGap(5)
{
    this->setFixedHeight(50);
    this->setMinimumWidth(800);
//    this->setStyleSheet("background-color:royalblue"); // rgb(65,105,225)
}

TimeBar::~TimeBar()
{

}

QPoint TimeBar::getCurDialPos()
{
    int xStartPos = _reckText.width() + _reckText.x() * 2; // 刻度线开始位置
    int curDialValue = _curDialValue;
    int numScale = (this->width()- xStartPos) / _dialGap;
    int yStartPos = _reckText.y() + _reckText.height();
    QPoint result(-1, 0);
    if (curDialValue >= _curWheelValue && curDialValue < _curWheelValue + numScale)
    {
        int xAdd = (curDialValue - _curWheelValue) * _dialGap;
        result = QPoint(xStartPos + xAdd , yStartPos + 3);
    }
    return result;
}

void TimeBar::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::RightButton)
    {
        return;
    }

    int xStartPos = _reckText.width() + _reckText.x() * 2; // 刻度线开始位置
    int dialXPos = (_curDialValue - _curWheelValue) * _dialGap + xStartPos;

    if (abs(event->pos().x() - dialXPos) < _dialGap)
    {
        _isDragging = true;
    }
}

void TimeBar::mouseReleaseEvent(QMouseEvent *)
{
    _isDragging = false;
}

//#include <QDebug>
void TimeBar::mouseDoubleClickEvent(QMouseEvent *event)
{
//    qDebug() << event->pos().x();
    setCursorXPos(event->pos().x());
}

void TimeBar::mouseMoveEvent(QMouseEvent *event)
{
    if (_isDragging)
    {
        int xStartPos = _reckText.width() + _reckText.x() * 2; // 刻度线开始位置
        int cursorXPos = event->pos().x();
        if (cursorXPos < xStartPos)
        {
            addWheelValue(-1);
            cursorXPos = xStartPos;
        }
        else if (cursorXPos > this->width())
        {
            addWheelValue(1);
            cursorXPos = this->width() - _dialGap;
        }
        setCursorXPos(cursorXPos);
    }
}

#ifndef QT_NO_WHEELEVENT
void TimeBar::wheelEvent(QWheelEvent *event)
{
    addWheelValue(event->delta() / 6);
}
#endif

void TimeBar::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);

    painter.setRenderHint(QPainter::Antialiasing, true);    // 反走样

    /* 绘制背景颜色 */
    painter.setPen(QPen(QColor("royalblue"), 3));    // 设置画笔颜色、宽度
    painter.setBrush(QColor("royalblue"));  // 设置画刷颜色 //darkcyan	#008B8B	0,139,139
    painter.drawRect(0, 0, this->width(), this->height());


    /* 绘制矩形 */
    painter.setPen(QPen(QColor("white"), 3));    // 设置画笔颜色、宽度
    painter.setBrush(QColor("darkcyan"));  // 设置画刷颜色 //darkcyan	#008B8B	0,139,139
    painter.drawRect(_reckText);

    /* 绘制文本 当前时间 */
    painter.setPen(QColor("white")); // 设置画笔颜色
    QFont font; // 设置字体：微软雅黑、点大小50、斜体
//    font.setFamily("Microsoft YaHei");
    font.setPointSize(13);
    painter.setFont(font);
    painter.drawText(QRect(_reckText.x() + _textSideGap, _reckText.y(), _reckText.width(), _reckText.height()),
                     Qt::AlignLeft | Qt::AlignVCenter, QStringLiteral("当前时间"));
    int showTime = _curDialValue * 10;
    painter.drawText(QRect(_reckText.x() + _textSideGap, _reckText.y(), _reckText.width() - _textSideGap * 2, _reckText.height()),
                     Qt::AlignRight | Qt::AlignVCenter,
                     QString::number(int(_curDialValue)).append(".").append(QString::number(showTime % 10)).append(QStringLiteral("  秒")));

    /* 绘制刻度线 */
    painter.setPen(QPen(QColor("white"), 1.8));
    int xStartPos = _reckText.width() + _reckText.x() * 2; // 刻度线开始位置
    int yStartPos = _reckText.y() + _reckText.height();
    // 从开始位置开始，绘制5000，基础平面高度在40处，最短刻度为7，刻度间隔为5
    int dialHeight = _reckText.height() / 4;
    int textRectSide = _reckText.height(); // 字体大小
    int numScale = (this->width()- xStartPos) / _dialGap;
    for (int i = 0; i < numScale; ++i)
    {
        QString showNum;
        int xPos = xStartPos + i * _dialGap;
        int yHeight = dialHeight;
        int dialShow = i + _curWheelValue;
        if (dialShow > _maxDialValue)
        {
            break;
        }
        if (dialShow % 60 == 0)
        {
            yHeight = dialHeight * 3;
            if (dialShow / 60 > 0)
            {
                showNum = QString::number(dialShow/60).append(QStringLiteral("分"));
            }else {
                showNum = QString::number(dialShow);
            }
        }
        else if (dialShow % 5 == 0)
        {
            yHeight = dialHeight * 2;
            if (dialShow % 10 == 0)
            {
                showNum = QString::number(dialShow);
            }
        }
        if (!showNum.isEmpty())
        {
            painter.drawText(QRect(xPos-textRectSide, yStartPos - dialHeight * 3 - textRectSide * 2, textRectSide * 2, textRectSide * 2),
                                 Qt::AlignBottom | Qt::AlignHCenter, showNum);
        }
        painter.drawLine(QPoint(xPos, yStartPos), QPoint(xPos, yStartPos - yHeight));
    }

    /* 绘制滑块 */
    int curDialValue = _curDialValue;
    if (curDialValue >= _curWheelValue && curDialValue < _curWheelValue + numScale)
    {
        painter.setPen(QPen(QColor("red"), 1.8));    // 设置画笔颜色、宽度
        painter.setBrush(QColor(255, 69, 0, 128));  // 设置画刷颜色 orangered	#FF4500	255,69,0
        int xAdd = (curDialValue - _curWheelValue) * _dialGap;
        painter.drawRect(xStartPos + xAdd - _dialGap, yStartPos + 3, _dialGap * 2, -dialHeight * 3 - 3);
        painter.drawLine(QPoint(xStartPos + xAdd , yStartPos + 3), QPoint(xStartPos + xAdd, this->height()));
    }
}

void TimeBar::addWheelValue(int wheelDelta)
{
    int numScale = (this->width() - 20 - 230) / _dialGap;
    if ( wheelDelta == 0 || (_curWheelValue <= _minDialValue && wheelDelta < 0)
     || (_curWheelValue + numScale >= _maxDialValue && wheelDelta > 0))
    {
        return;
    }

    if (_curWheelValue > _minDialValue && _curWheelValue + wheelDelta < _minDialValue)
    {
        _curWheelValue = _minDialValue;
        repaint();
        return;
    }

    if (_curWheelValue + numScale < _maxDialValue && _curWheelValue + wheelDelta + numScale > _maxDialValue)
    {
        _curWheelValue = _maxDialValue - numScale;
        repaint();
        return;
    }

    _curWheelValue += wheelDelta;
    repaint();
    emit signalRepaintDialPos(getCurDialPos());
}

void TimeBar::setCursorXPos(int xPos)
{
    int xStartPos = _reckText.width() + _reckText.x() * 2; // 刻度线开始位置
    if (xPos < xStartPos)
    {
        return;
    }

    _curDialValue = _curWheelValue + (xPos - xStartPos) / _dialGap;
    if (_curDialValue > _maxDialValue)
    {
        _curDialValue = _maxDialValue;
    }
    repaint();

    emit signalRepaintDialPos(getCurDialPos());
}
